﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class CompanyPartnerCategoryListControl : YuanlinUserControl,
        IReplyConverter<CompanyPartnerCategoryReply, GetListOperation>
    {
        protected Repeater list;
        protected Pager pager;
        protected CurrentPage currentPage;
        private EntityList companyPartnerCategories;
        private YuanlinUser user = HttpContext.Current.User as YuanlinUser;

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            if (!Page.IsPostBack && !AjaxManager.IsCallBack)
            {
                CompanyPartnerCategoryListRequest request = new CompanyPartnerCategoryListRequest();
                request.PageIndex = pager.PageIndex;
                request.PageSize = pager.PageSize;
                request.UserId = user.EntityId.Value;
                requestBinders.Add(RequestHelper.BuildGetCompanyPartnerCategoryListRequest(this, request));
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack && !AjaxManager.IsCallBack)
            {
                BindData();
            }
            AjaxManager.Register(this, "AjaxMethod");
        }

        private void BindData()
        {
            if (list == null)
            {
                return;
            }

            list.DataSource = companyPartnerCategories;
            list.DataBind();

            if (pager != null && currentPage != null)
            {
                pager.TotalPages = pager.CalculateTotalPages(companyPartnerCategories.TotalCount);
                currentPage.TotalRecords = (companyPartnerCategories.TotalCount);
                currentPage.TotalPages = pager.TotalPages;
                currentPage.PageIndex = pager.PageIndex;
            }
        }

        #region Ajax Method

        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public void DeleteCompanyPartnerCategory(int companyPartnerCategoryId)
        {
            BusinessManager.DeleteCompanyPartnerCategory(companyPartnerCategoryId);
        }

        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public int DeleteCompanyPartnerCategories(string entityIds)
        {
            if (string.IsNullOrEmpty(entityIds))
            {
                return 0;
            }
            string[] entityIdArray = entityIds.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            int entityId = 0;
            foreach (string stringEntityId in entityIdArray)
            {
                try
                {
                    entityId = Int32.Parse(stringEntityId);
                }
                catch
                {
                    continue;
                }
                if (entityId > 0)
                {
                    DeleteCompanyPartnerCategory(entityId);                
                }
            }
            return 0;
        }

        #endregion


        #region IReplyConverter<CompanyPartnerCategoryReply,GetListOperation> Members

        public void ConvertReply(CompanyPartnerCategoryReply reply, GetListOperation operation)
        {
            if (reply != null)
            {
                companyPartnerCategories = reply.EntityList;

                if (!Page.IsPostBack && !AjaxManager.IsCallBack)
                {
                    BindData();
                }
            }
        }

        #endregion
    }
}